import { IHttpResponse, IObject } from "~/types/interface";
import http from "~/utils/request";

/**
 * 常用CRUD
 */
export default {
    /**
     * 删除
     * @param path
     * @param params
     * @returns
     */
    delete(path: string, params: IObject): Promise<IHttpResponse> {
        return http({
            url: path,
            data: params,
            method: "DELETE"
        });
    },
    get(path: string, params?: IObject, headers?: IObject): Promise<IHttpResponse> {
        return new Promise((resolve, reject) => {
            http({
                url: path,
                params,
                headers,
                method: "GET"
            })
                .then(resolve)
                .catch((error) => {
                    if (error !== "-999") {
                        reject(error);
                    }
                });
        });
    },
    put(path: string, params?: IObject, headers?: IObject): Promise<IHttpResponse> {
        return http({
            url: path,
            data: params,
            headers: {
                "Content-Type": "application/json;charset=UTF-8",
                ...headers
            },
            method: "PUT"
        });
    },
    /**
     * 通用post方法
     * @param path
     * @param body
     * @returns
     */
    post(path: string, body?: IObject, headers?: IObject): Promise<IHttpResponse> {
        return http({
            url: path,
            method: "post",
            headers: {
                "Content-Type": "application/json;charset=UTF-8",
                ...headers
            },
            data: body
        });
    }
};
